package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import models.Batch;
import models.Commodity;
import models.Message;
import models.QueryBean;
import models.ShoppingCar;
import models.User;
import models.BaseEntity.PeopleType;
import models.BaseEntity.Status;
import models.Message.MessageStatus;
import models.Message.MessageType;
import play.data.validation.Valid;
import play.mvc.Controller;
import play.mvc.With;
import service.CommoditiesService;
import service.MessageService;

@With(Secure.class)
public class UserMessage extends Controller {
	
	public static void index(QueryBean queryBean) {
		if(queryBean == null) {
			queryBean = new QueryBean();
		}
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		List<Object> objs = new ArrayList<Object>();
		String hql ="status != ? and target = null and ((creatorType = ? and createByUser = ?) or toUser =?) order by lastModifiedAt desc";
		objs.add(Status.DISABLED);
		objs.add(PeopleType.USER);
		objs.add(user);
		objs.add(user);
		
		queryBean.totalQuantity = Message.count(hql, objs.toArray());
		queryBean.pageCaculate();
		
		List<Message> msgs = Message.find(hql, objs.toArray()).fetch((int)queryBean.currentPage, queryBean.quantityEachPage);
		render("@users.messages", msgs, queryBean);
	}
	
	public static void reply(long id, String content) {
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		
		MessageService.userReply(id, content, user);
	}
	
	/**
	 * 跳转到新增Message页面。
	 */
	public static void newMessage() {
		Message msg = new Message();
		msg.type = MessageType.CONSULTING;
		render("@users.newMessage", msg);
	}
	
	/**
	 * 发送消息。
	 * @param msg
	 */
	public static void sendMessage(@Valid Message msg) {
		if(validation.hasErrors()) {
			params.flash(); // add http parameters to the flash scope
//			validation.keep(); // keep the errors for the next request
			render("@users.newMessage", msg);
		}
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		MessageService.userSendMessage(msg.content, msg.type, user);
		index(null);
	}
	
	public static long countNewMessage() {
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		
		String hql = "status != ? and toUser.id = ? and isread = ?";
		List<Object> objs = new ArrayList<Object>();
		objs.add(Status.DISABLED);
		objs.add(user.id);
		objs.add(false);
		return Message.count(hql, objs.toArray());
	}
	
	/**
	 * 把已阅读的信息标记为已读状态。
	 */
	public static void markReadMessage() {
		int i = 0;
		while (true) {
			String idStr = request.params.get("unReadId[" + i + "]");
			if(!ValidationUtil.isLong(idStr)) break;
			
			Message msg = Message.findById(Long.parseLong(idStr));
			if(msg != null) {
				msg.isread = true;
				msg.save();
			}
			i++;
		}
	}
}

